<template>
  <div v-if="dataset">
    <el-row>
      <el-col :span="22">
        <input
          v-if="auditData.data.length == 0 || auditData.data[0].FAUDIT != 0"
          class="input_inner"
          placeholder="请输入内容"
          v-model="supervisionInput"
          @keyup.enter="querySupervisionCode"
          @focus="focus($event)"
          ref="searchInput"
        />
      </el-col>
      <el-col :span="2">
        <el-button
          v-if="auditData.data.length == 0 || auditData.data[0].FAUDIT != 0"
          type="primary"
          style="position: absolute; right: 40px; top: 22px"
          @click="clearTable(dataset.data)"
          >清空</el-button
        >
      </el-col>
    </el-row>
    <div style="height: 360px">
      <el-table :data="dataset.data" max-height="360" style="width: 100%">
        <el-table-column type="index" width="50" />
        <el-table-column prop="FSUPERVISIONCODE" label="电子监管码" />
        <el-table-column prop="FBARCODE" label="条形码" />
        <el-table-column prop="FDESC" label="通用名称" />
        <el-table-column
          v-if="auditData.data.length == 0 || auditData.data[0].FAUDIT != 0"
          fixed="right"
          label="操作"
          width="80"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              title="删除"
              size="small"
              @click="remove(scope.$index, scope.row)"
            >
              <i class="el-icon-circle-close"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import baseView from "@/views/base";
import { dataHelper } from "hi-ui";
import { cloneDeep } from "lodash";

export default {
  components: {},
  extends: baseView,
  props: {
    dataset: Object,
    codeData: Object,
    auditData: Object,
    pmodel: Object,
    kind: String,
  },
  data() {
    return {
      supervisionInput: null,

      // 拷贝数据
      curRecData: [],
    };
  },
  methods: {
    querySupervisionCode() {
      // debugger;
      let val = this.supervisionInput;
      var curRec = this.dataset.data.find((item) => {
        return item["FSUPERVISIONCODE"] == val;
      });
      if (curRec) {
        this.$message.error("【" + val + "】电子监管码已存在");
        this.$refs.searchInput.select();
        return;
      }
      var curRec1 = this.curRecData.find((item) => {
        return item["FSUPERVISIONCODE"] == val;
      });
      if (curRec1) {
        this.dataset.add({
          FSUPERVISIONCODE: val,
          FDESC: this.codeData["FDESC"],
        });
      } else {
        //处理采购退货
        if (this.kind == "return") {
          let order = this.pmodel.data.FINSPECTNUMBER;
          let barcode = this.pmodel.data.FBARCODE;
          let param = new dataHelper.queryHelper.Param();
          let where = new dataHelper.queryHelper.Where({
            criterionData: {
              join: "and",
              items: [
                {
                  sign: "eq",
                  name: "FSUPERVISIONCODE",
                  tablefilter: false,
                  dataType: "dbString",
                  value: val,
                  enabled: true,
                },
                {
                  sign: "eq",
                  name: "FNUMBER",
                  tablefilter: false,
                  dataType: "dbString",
                  value: order,
                  enabled: true,
                },
                {
                  sign: "eq",
                  name: "FBARCODE",
                  tablefilter: false,
                  dataType: "dbString",
                  value: barcode,
                  enabled: true,
                },
              ],
            },
          });
          param.setWhere(where);
          this.queryEntity(
            param,
            "supervisioncode/dataset/SupervisioncodeEntity_in_return.xml"
          ).then((res) => {
            debugger;
            if (res.dataPack.rows.length == 0) {
              this.$message.error("没有找到电子监管码" + "【" + val + "】");
            }
            let durgstore = res.dataPack.rows[0];
            if (durgstore) {
              let data = {};
              let returnField = {
                FSUPERVISIONCODE: "FSUPERVISIONCODE",
                FDESC: "FDESC",
                FBARCODE: "FBARCODE",
              };
              for (let fn in returnField) {
                data[fn] = durgstore[returnField[fn]];
              }
              this.dataset.add(data);
            }
          });
        }
        // 处理商品拆分
        else if (this.kind == "split") {
          if (this.codeData.data.length == 0) {
            this.$message.error("请选择数据");
            this.$refs.searchInput.select();
            return;
          }
          let barcode = this.codeData.data[0].FBARCODE;
          let param = new dataHelper.queryHelper.Param();
          let where = new dataHelper.queryHelper.Where({
            criterionData: {
              join: "and",
              items: [
                {
                  sign: "eq",
                  name: "FSUPERVISIONCODE",
                  tablefilter: false,
                  dataType: "dbString",
                  value: val,
                  enabled: true,
                },
                {
                  sign: "eq",
                  name: "FBARCODE",
                  tablefilter: false,
                  dataType: "dbString",
                  value: barcode,
                  enabled: true,
                },
              ],
            },
          });
          param.setWhere(where);
          this.queryEntity(
            param,
            "supervisioncode/dataset/SupervisioncodeEntity_query.xml"
          ).then((res) => {
            // debugger;
            if (res.dataPack.rows.length == 0) {
              this.$message.error("没有找到电子监管码" + "【" + val + "】");
            }
            let durgstore = res.dataPack.rows[0];
            if (durgstore) {
              let data = {};
              let returnField = {
                FSUPERVISIONCODE: "FSUPERVISIONCODE",
                FDESC: "FDESC",
                FBARCODE: "FBARCODE",
              };
              for (let fn in returnField) {
                data[fn] = durgstore[returnField[fn]];
              }
              this.dataset.add(data);
            }
          });
        } else {
          let barcode = this.pmodel.data.FBARCODE;
          let param = new dataHelper.queryHelper.Param();
          let where = new dataHelper.queryHelper.Where({
            criterionData: {
              join: "and",
              items: [
                {
                  sign: "eq",
                  name: "FSUPERVISIONCODE",
                  tablefilter: false,
                  dataType: "dbString",
                  value: val,
                  enabled: true,
                },
                {
                  sign: "eq",
                  name: "FBARCODE",
                  tablefilter: false,
                  dataType: "dbString",
                  value: barcode,
                  enabled: true,
                },
              ],
            },
          });
          param.setWhere(where);
          this.queryEntity(
            param,
            "supervisioncode/dataset/SupervisioncodeEntity_query.xml"
          ).then((res) => {
            if (res.dataPack.rows.length == 0) {
              this.$message.error("没有找到电子监管码" + val);
            }
            let durgstore = res.dataPack.rows[0];
            if (durgstore) {
              let data = {};
              let returnField = {
                FSUPERVISIONCODE: "FSUPERVISIONCODE",
                FDESC: "FDESC",
                FBARCODE: "FBARCODE",
              };
              for (let fn in returnField) {
                data[fn] = durgstore[returnField[fn]];
              }
              this.dataset.add(data);
            }
          });
        }
      }

      this.$refs.searchInput.select();
    },

    focus(event) {
      event.currentTarget.select();
    },
    remove(index, data) {
      this.dataset.remove(data);
    },
    clearTable(data) {
      // debugger;
      // if (this.codeData.data.length == 0) return;
      this.dataset.remove(data);
    },
  },
  mounted() {
    // debugger;
    //创建主数据集
    // this.dataset = this.$dsContainer.createDataSet(this.datasetCfg);
  },
  created() {
    // debugger;
    this.curRecData = cloneDeep(this.dataset.data);
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
.input_inner {
  height: 36px;
  line-height: 36px;
  margin: 0 0 20px 50px;
  padding-left: 10px;
  outline: none;
}
</style>
